package com.sskj.b70.v.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxCompoundButton;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.common.rxbus2.RxBusCode;
import com.sskj.b70.common.rxbus2.Subscribe;
import com.sskj.b70.common.rxbus2.ThreadMode;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.m.bean.PayWayBean;
import com.sskj.b70.m.bean.UserInfo;
import com.sskj.b70.p.activity.SecurityCenterPresenter;
import com.sskj.b70.utils.BtnDoubleClickUtil;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.MD5Utils;
import com.sskj.b70.utils.PictureUtil;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sskj.b70.common.Constants.PAY_PWD_SETTING_CODE;
import static com.sskj.b70.common.Constants.VERIFY_IDENT_CODE;

/**
 * 安全中心
 * create by Hey at 2018/4/20 15:04
 */
public class SecurityCenterActivity extends BaseActivity<SecurityCenterPresenter> implements View.OnClickListener {


    @BindView(R.id.security_mobile_checked)
    TextView mSecurityMobileChecked;
    @BindView(R.id.security_bottom_recyclerview)
    RecyclerView mSecurityRecyclerView;
    @BindView(R.id.security_email_checked)
    TextView mSecurityEmailChecked;
    @BindView(R.id.security_login_paw_checked)
    TextView mSecurityLoginPawChecked;
    @BindView(R.id.security_pay_psw_checked)
    TextView mSecurityPayPswChecked;
    @BindView(R.id.security_verify_checked)
    TextView mSecurityVerifyChecked;
    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.security_apply_business_checked)
    TextView mSecurityApplyBusinessChecked;
    @BindView(R.id.switchGesture)
    Switch switchGesture;
    @BindView(R.id.security_apply_text_gusture)
    TextView tvGesture;
    @BindView(R.id.security_apply_gusture)
    FrameLayout flGesture;
    @BindView(R.id.flGesturePwd)
    FrameLayout flGesturePwd;
    private UserInfo mUserInfo;
    private BaseAdapter<PayWayBean> mAdapter;
    /**
     * 用户选择的支付方式
     */
    private String mSelectName;
    private EditText mDialogInputBankName;
    private EditText mDialogInputBankCardNum;
    private View mDialogThirdpartyLayout;
    private TextView mDialogThirdpartyName;
    private EditText mDialogInputThirdpartyAccount;
    private ImageView mDialogThirdpartyPic;
    private Spinner mDialogPayWaySpinner;
    private TextView mDialogRealname;
    private View mDialogCancel;
    private View mDialogSubmit;   //确定按钮
    private View mDialogBanknumLayout;
    private AlertDialog mAlertDialog;
    private View mDialogClose;
    private EditText mDialogInputPayPwd;
    private View mFooterView;  // 添加支付方式
    private View mFailView;
    private View mStartLading;
    private EditText mDialogInputBankBranch;
    private TextView mDialogThirdpartyPicHint;
    /**
     * 支付方式 微信，支付宝 收款二维码 图片URL
     */
    private String mImagePath;
    /**
     * 支付方式添加 或 编辑  add/edit
     */
    private String mType = "add";
    private View mLoadingProgress;
    private boolean isFirst;
    private String filePath;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_security_center;
    }

    @Override
    public SecurityCenterPresenter getPresenter() {
        return new SecurityCenterPresenter();
    }

    @Override
    protected void onViewBinding() {
        RxBus.getDefault().register(this);
        isFirst = true;
        boolean isGestureOn = (boolean) SPUtils.get(Constants.IS_GESTURE_ON, false, MyAppLication.getID());
        boolean isGestureSet = (boolean) SPUtils.get(Constants.IS_GESTURE_SET, false, MyAppLication.getID());
        if (isGestureSet) {
            tvGesture.setVisibility(View.GONE);
            flGesturePwd.setVisibility(View.VISIBLE);
            switchGesture.setVisibility(View.VISIBLE);
            switchGesture.setChecked(isGestureOn);
            RxCompoundButton.checkedChanges(switchGesture)
                    .subscribe(isCheck -> {
                        SPUtils.put(Constants.IS_GESTURE_ON, isCheck, MyAppLication.getID());
                        if (isFirst) {
                            isFirst = false;
                            return;
                        }
                        ToastUtil.showShort(isCheck ? App.INSTANCE.getString(R.string.strSecurityCenterActivity1) : App.INSTANCE.getString(R.string.strSecurityCenterActivity2));
                    });
        } else {
            flGesturePwd.setVisibility(View.GONE);
            tvGesture.setVisibility(View.VISIBLE);
            switchGesture.setVisibility(View.GONE);
            ClickUtil.click(flGesture, () -> {
                LockActivity.start(this);
            });
        }
        mToolBar.setLeftButtonOnClickLinster(v -> finish());

        mSecurityRecyclerView.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
//        mSecurityRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        mAdapter = new BaseAdapter<PayWayBean>(R.layout.layout_pay_way, null, mSecurityRecyclerView) {

            @Override
            public void bind(ViewHolder holder, PayWayBean item) {
//                holder.setText(R.id.item_type_name, item.getTip());  // 支付方式 例如：微信
                switch (item.getType()) {
                    case "1":
                        //微信
                        holder.setImageResource(R.id.imageView, R.mipmap.wei_xin);
                        holder.setText(R.id.item_type_name, App.INSTANCE.getString(R.string.strSecurityCenterActivity4));
                        holder.setText(R.id.item_content, String.format("%1$s", item.getWxAccount()));   // 手机号  姓名  支付方式
                        break;
                    case "2":
                        //支付宝
                        holder.setImageResource(R.id.imageView, R.mipmap.zhifu_bao);

                        holder.setText(R.id.item_type_name, App.INSTANCE.getString(R.string.strSecurityCenterActivity7));
                        holder.setText(R.id.item_content, String.format("%1$s", item.getAlipayAccount()));   // 手机号  姓名  支付方式
                        break;
                    case "3":
                        // 银行卡
                        holder.setImageResource(R.id.imageView, R.mipmap.icon_bank);

                        holder.setText(R.id.item_type_name, App.INSTANCE.getString(R.string.strSecurityCenterActivity10));
                        holder.setText(R.id.item_content, String.format("%1$s", item.getBankCardNo()));   // 手机号  姓名  支付方式
                        break;
                }

                holder.setOnClickListener(R.id.item_edit, v -> {//修改支付方式
                    showDialog(item);
                });
                holder.setOnClickListener(R.id.item_switch, v -> {//切换支付方式开关
                    mPresenter.requsetCheckSwitch(item);
                });
//                if (item.getStatus() == 0) {
//                    holder.setChecked(R.id.item_switch, false);
//                } else {
//                    holder.setChecked(R.id.item_switch, true);
//                }

                if (item.isDisable()) {
                    holder.setChecked(R.id.item_switch, false);
                } else {
                    holder.setChecked(R.id.item_switch, true);
                }
                if ("wx".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_wx);
                } else if ("alipay".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_ali);
                } else if ("backcard".equals(item.getType())) {
                    holder.setImageResource(R.id.imageView, R.mipmap.icon_bank);
                }
            }
        };
        //新增支付方式view
        mFooterView = LayoutInflater.from(this).inflate(R.layout.item_text_security, null);
        mFooterView.setOnClickListener(v -> {
            if (checkIdent()) {  // 该方法用于判断用户是否实名认证
                if (!TextUtils.isEmpty(mUserInfo.tpwd)) {  // 判断用户是否设置交易密码
                    showDialog(null);
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strSecurityCenterActivity15));
                    openActivity(PayPwdSettingActivity.class, PAY_PWD_SETTING_CODE);
                }
            }
        });
        //网络链接失败，重新加载view
        mFailView = LayoutInflater.from(this).inflate(R.layout.layout_reset_loading, null);
        mStartLading = mFailView.findViewById(R.id.start_loading);
        mLoadingProgress = mFailView.findViewById(R.id.loading_progress);
        mStartLading.setOnClickListener(v -> {
            mStartLading.setVisibility(View.INVISIBLE);
            mLoadingProgress.setVisibility(View.VISIBLE);
            mPresenter.requsetPayWayList();
        });

        mPresenter.requsetPayWayList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.request();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Constants.BIND_EMAIL_CODE:
                    if (mUserInfo != null) {
                        String email = data.getStringExtra("resultData");
                        mUserInfo.email = email;
                        mSecurityEmailChecked.setText(email);
//                        RxBus.getDefault().post(mUserInfo);
                    }
                    break;
                case Constants.PAY_PWD_SETTING_CODE:   // 设置交易密码
                    if (mUserInfo != null) {
                        mUserInfo.tpwd = "1";
                        mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity18));
//                        RxBus.getDefault().post(mUserInfo);
                    }
                    break;
                case VERIFY_IDENT_CODE:   // 实名认证
                    if (mUserInfo != null) {
                        int status = data.getIntExtra("resultData", 1);
                        if (status == 2) {
                            mUserInfo.status = 2;
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, 2);
                            mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity20));
                        }
//                        RxBus.getDefault().post(mUserInfo);
                    }
                    break;
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    mImagePath = selectList.get(0).getPath();
                    mDialogThirdpartyPic.setImageBitmap(PictureUtil.getSmallBitmap(selectList.get(0).getPath()));
                    mPresenter.uploadImg(new File(mImagePath));
                    break;
            }
        } else if (requestCode == Constants.ACTIVITY_LOCK && resultCode == Constants.GESTURE_PASSWD_OK) {
            tvGesture.setVisibility(View.GONE);
            switchGesture.setVisibility(View.VISIBLE);
            switchGesture.setChecked(true);
            flGesturePwd.setVisibility(View.VISIBLE);
            ToastUtil.showShort(App.INSTANCE.getString(R.string.strSecurityCenterActivity1));
            RxCompoundButton.checkedChanges(switchGesture)
                    .subscribe(isCheck -> {
                        SPUtils.put(Constants.IS_GESTURE_ON, isCheck, MyAppLication.getID());
                        ToastUtil.showShort(isCheck ? App.INSTANCE.getString(R.string.strSecurityCenterActivity1) : App.INSTANCE.getString(R.string.strSecurityCenterActivity2));
                    });
            ClickUtil.click(flGesture, () -> {//取消点击事件
            });
        }
    }

    @Subscribe(code = RxBusCode.USER_INFO_UPDATE, threadMode = ThreadMode.MAIN)
    public void updateUser() {
        mPresenter.request();
    }

    @OnClick({R.id.security_apply_business_layout, R.id.security_mobile_layout, R.id.flGesturePwd, R.id.security_email_layout, R.id.security_login_paw_layout, R.id.security_pay_psw_layout, R.id.security_verify_layout})
    public void onViewClicked(View view) {
        if (mUserInfo != null && !BtnDoubleClickUtil.isFastDoubleClick()) {
            switch (view.getId()) {
                case R.id.security_mobile_layout:
                    if (TextUtils.isEmpty(mUserInfo.mobile)) {
                        BindMobileActivity.start(this);
                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.strSecurityCenterActivity24));
                    }
                    break;
                case R.id.security_email_layout:
                    openActivity(BindEmailActivity.class, Constants.BIND_EMAIL_CODE);
                    break;
                case R.id.security_login_paw_layout:
                    if (!TextUtils.isEmpty(mUserInfo.getMobile())) {
                        Intent intent = new Intent(mActivity, PwdResetActivity.class);
                        intent.putExtra(Constants.IS_RESET, false);
                        startActivity(intent);
                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mobile_first));
                    }
                    break;
                case R.id.security_pay_psw_layout:
                    if (!TextUtils.isEmpty(mUserInfo.getMobile())) {
                        openActivity(PayPwdSettingActivity.class, PAY_PWD_SETTING_CODE);
                    } else {
                        ToastUtil.showShort(App.INSTANCE.getString(R.string.mobile_first));
                    }
                    break;
                case R.id.security_verify_layout:
                    checkIdentAndInto();
                    break;
                case R.id.flGesturePwd://更新手势密码
                    LockActivity.update(this);
                    break;
                case R.id.security_apply_business_layout:  // 商家认证
                    if (checkIdent()) {
                        if (!TextUtils.isEmpty(mUserInfo.tpwd)) {  // 交易密码
                            if (mUserInfo.isIs_shop()) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogStyleBottom);
                                View inflater = LayoutInflater.from(mActivity).inflate(R.layout.dialog_relieve_business, null);
                                AlertDialog alertDialog = builder.setView(inflater).show();
                                inflater.findViewById(R.id.dialog_relieve_cancel).setOnClickListener(v -> alertDialog.dismiss());
                                inflater.findViewById(R.id.dialog_relieve_confirm).setOnClickListener(v -> mPresenter.requestRelieveBusiness(alertDialog));
                            } else {
                                SellerApplyActivity.start(this);
                            }
                        } else {
                            ToastUtil.showShort(App.INSTANCE.getString(R.string.strSecurityCenterActivity25));
                        }

                    }
                    break;
            }
        }
    }

    public void updataStatus(UserInfo user) {
        mUserInfo = user;
//        if (!TextUtils.isEmpty(user.mail)/* && CheckUtil.isEmail(user.mail)*/) {
//            mSecurityEmailChecked.setText(user.mail);
//        } else {
//            mSecurityEmailChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity26));
//        }

        mSecurityLoginPawChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity18));   //登录密码
        if (!TextUtils.isEmpty(user.mobile)) {
            mSecurityMobileChecked.setText(App.INSTANCE.getString(R.string.xmlactivity_security_center42));
        } else {
            mSecurityMobileChecked.setText(App.INSTANCE.getString(R.string.unbind));
        }
        if (!TextUtils.isEmpty(user.email)) {
            mSecurityEmailChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity18));
        } else {
            mSecurityEmailChecked.setText(App.INSTANCE.getString(R.string.unbind));
        }

        if (!TextUtils.isEmpty(user.tpwd)) {  // 交易密码
            mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity18));
        } else {
            mSecurityPayPswChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity29));
        }

        switch (user.status) {       // 实名认证
            case 1:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity30));
                break;
            case 2:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity20));
                break;
            case 3:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity32));
                break;
            case 4:
                mSecurityVerifyChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity33));
                //                mSecurityVerifyChecked.setTextColor();
                break;
        }

        if (user.isIs_shop()) {    // 商家认证
            mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity34));
        } else {
            mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity30));
        }
    }

    public void showDialog(PayWayBean item) {
        filePath = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyDialogStyleBottom);
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_edit_pay_way, null);
        builder.setView(inflate);
        mDialogBanknumLayout = inflate.findViewById(R.id.dialog_banknum_layout);
        mDialogInputBankName = inflate.findViewById(R.id.dialog_input_bank_name);
        mDialogInputBankCardNum = inflate.findViewById(R.id.dialog_input_bankcard_num);
        mDialogInputBankBranch = inflate.findViewById(R.id.dialog_input_bank_branch_name);
        mDialogThirdpartyLayout = inflate.findViewById(R.id.dialog_thirdparty_layout);
        mDialogThirdpartyName = inflate.findViewById(R.id.thirdparty_name);
        mDialogInputThirdpartyAccount = inflate.findViewById(R.id.dialog_input_thirdparty_account);
        mDialogThirdpartyPic = inflate.findViewById(R.id.dialog_thirdparty_pic);
        mDialogThirdpartyPicHint = inflate.findViewById(R.id.dialog_thirdparty_pic_hint);
        mDialogPayWaySpinner = inflate.findViewById(R.id.dialog_pay_way_spinner);
        mDialogRealname = inflate.findViewById(R.id.dialog_realname);
        mDialogInputPayPwd = inflate.findViewById(R.id.dialog_input_pay_pwd);
        CheckBox checkBox = inflate.findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mDialogInputPayPwd.setTransformationMethod(new HideReturnsTransformationMethod());
            } else {
                mDialogInputPayPwd.setTransformationMethod(new PasswordTransformationMethod());
            }
        });
        mDialogClose = inflate.findViewById(R.id.dialog_close);
        mDialogCancel = inflate.findViewById(R.id.dialog_cancel);
        mDialogSubmit = inflate.findViewById(R.id.dialog_submit);
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, new String[]{App.INSTANCE.getString(R.string.strSecurityCenterActivity10), App.INSTANCE.getString(R.string.strSecurityCenterActivity4), App.INSTANCE.getString(R.string.strSecurityCenterActivity7)});
        mDialogPayWaySpinner.setAdapter(stringArrayAdapter);
        mDialogPayWaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSelectName = stringArrayAdapter.getItem(position);
                checkLayout();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                mDialogPayWaySpinner.setSelection(0, true);
                mSelectName = stringArrayAdapter.getItem(0);
                checkLayout();
            }
        });

        if (item != null) {
            mDialogPayWaySpinner.setEnabled(false);
            if ("3".equals(item.getType())) {
                mDialogPayWaySpinner.setSelection(0, true);
                mDialogInputBankName.setText(item.getBank());
                mDialogInputBankBranch.setText(item.getBranch());
                mDialogInputBankCardNum.setText(item.getNumber());
            } else if ("1".equals(item.getType())) {
                mDialogPayWaySpinner.setSelection(1, true);
                mDialogInputThirdpartyAccount.setText(item.getWxAccount());
            } else if ("2".equals(item.getType())) {
                mDialogPayWaySpinner.setSelection(2, true);
                mDialogInputThirdpartyAccount.setText(item.getAlipayAccount());
            }
            mDialogRealname.setText(MyAppLication.getName());

            mType = item.getId();
        } else {
            mType = null;
            mDialogPayWaySpinner.setEnabled(true);
            mDialogPayWaySpinner.setSelection(0, true);
            mDialogRealname.setText(MyAppLication.getName());
        }
        mDialogSubmit.setOnClickListener(this);
        mDialogClose.setOnClickListener(this);
        mDialogCancel.setOnClickListener(this);
        mDialogThirdpartyPic.setOnClickListener(this);
        mAlertDialog = builder.create();
        mAlertDialog.show();
    }

    private void checkLayout() {
        if (App.INSTANCE.getString(R.string.strSecurityCenterActivity10).equals(mSelectName)) {
            mDialogBanknumLayout.setVisibility(View.VISIBLE);
            mDialogThirdpartyLayout.setVisibility(View.GONE);
        } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity4).equals(mSelectName)) {
            mDialogBanknumLayout.setVisibility(View.GONE);
            mDialogThirdpartyLayout.setVisibility(View.VISIBLE);
            mDialogThirdpartyName.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity45));
            mDialogInputThirdpartyAccount.setHint(String.format(getString(R.string.thirdparty_account), App.INSTANCE.getString(R.string.strSecurityCenterActivity4)));
            mDialogThirdpartyPicHint.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity47));
        } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity7).equals(mSelectName)) {
            mDialogBanknumLayout.setVisibility(View.GONE);
            mDialogThirdpartyLayout.setVisibility(View.VISIBLE);
            mDialogThirdpartyName.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity49));
            mDialogInputThirdpartyAccount.setHint(String.format(getString(R.string.thirdparty_account), App.INSTANCE.getString(R.string.strSecurityCenterActivity7)));
            mDialogThirdpartyPicHint.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity51));
        }
    }

    @Override
    public void onClick(View v) {
        if (!BtnDoubleClickUtil.isFastDoubleClick()) {
            switch (v.getId()) {
                case R.id.dialog_submit:
                    String bankName = mDialogInputBankName.getText().toString().trim();
                    String bankBranch = mDialogInputBankBranch.getText().toString().trim();
                    String bankNum = mDialogInputBankCardNum.getText().toString().trim();
                    String account = mDialogInputThirdpartyAccount.getText().toString().trim();
                    String payPwd = mDialogInputPayPwd.getText().toString().trim();
                    String payName = "";
                    if (App.INSTANCE.getString(R.string.strSecurityCenterActivity10).equals(mSelectName)) {
                        if (TextUtils.isEmpty(bankName)) {
                            ToastUtil.showShort(R.string.empty_bank_name);
                            return;
                        }
                        if (TextUtils.isEmpty(bankBranch)) {
                            ToastUtil.showShort(R.string.empty_bank_branch);
                            return;
                        }
                        if (TextUtils.isEmpty(bankNum)) {
                            ToastUtil.showShort(R.string.empty_bank_card_num);
                            return;
                        }
                        if (bankNum.length() < 10 || bankNum.length() > 20) {
                            ToastUtil.showShort(R.string.error_bank_card_num);
                            return;
                        }
                        payName = "3";
                    } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity4).equals(mSelectName)) {
                        if (TextUtils.isEmpty(account)) {
                            ToastUtil.showShort(R.string.empty_wx_account);
                            return;
                        }
                        if (TextUtils.isEmpty(filePath)) {
                            ToastUtil.showShort(R.string.empty_pay_image_url);
                            return;
                        }
                        payName = "1";
                    } else if (App.INSTANCE.getString(R.string.strSecurityCenterActivity7).equals(mSelectName)) {
                        if (TextUtils.isEmpty(account)) {
                            ToastUtil.showShort(R.string.empty_ali_account);
                            return;
                        }
                        if (TextUtils.isEmpty(filePath)) {
                            ToastUtil.showShort(R.string.empty_pay_image_url);
                            return;
                        }
                        payName = "2";
                    }
                    if (TextUtils.isEmpty(payName)) {
                        ToastUtil.showShort(R.string.empty_pay_way_name);
                        return;
                    }
                    if (TextUtils.isEmpty(payPwd)) {
                        ToastUtil.showShort(R.string.empty_pay_pwd);
                        return;
                    }
                    // 传送字段依次为：：添加修改字段，支付名字，开户银行，银行支行，银行卡号，account,图片路径，资金密码
//                    mPresenter.requestAddPayWay(mType, payName, bankName, bankBranch, bankNum, account, mImagePath, MD5Utils.getMd5Value(payPwd));

                    mPresenter.addPayWay(mType, payName, MD5Utils.getMd5Value(payPwd),
                            payName.equals("1") ? account : null, payName.equals("1") ? filePath : null,
                            payName.equals("2") ? account : null, payName.equals("2") ? filePath : null,
                            payName.equals("3") ? bankNum : null, payName.equals("3") ? bankName : null, payName.equals("3") ? bankBranch : null);
                    break;
                case R.id.dialog_cancel:
                case R.id.dialog_close:
                    if (mAlertDialog != null) {
                        mAlertDialog.dismiss();
                    }
                    break;
                case R.id.dialog_thirdparty_pic://选择二维码图片
                    PictureSelector.create(this)
                            .openGallery(PictureMimeType.ofImage())
                            .selectionMode(PictureConfig.SINGLE)
                            .forResult(PictureConfig.CHOOSE_REQUEST);
                    break;
            }
        }
    }

    public void onPayWaySuccess(List<PayWayBean> list) {
        mAdapter.removeHeaderView(mFailView);
        if (list.size() < 3) {
            if (mAdapter.getFooterLayoutCount() == 0) {
                mAdapter.addFooterView(mFooterView);
            }
        }
        mAdapter.setNewData(list);
    }

    public void onHttpFail() {
        if (mAdapter.getHeaderLayoutCount() == 0) {
            mAdapter.addHeaderView(mFailView);
        } else {
            mStartLading.setVisibility(View.VISIBLE);
            mLoadingProgress.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 当支付方式开关切换成功
     *
     * @param item
     */
    public void onChangeSwitchSuccess(PayWayBean item) {
        mAdapter.notifyDataSetChanged();
    }

    /**
     * 当添加或修改支付方式成功
     */
    public void onChangePayWaySuccess() {
        mImagePath = "";
        if (mAlertDialog != null) {
            mAlertDialog.dismiss();
        }
    }

    public void onRelieveBusinessSuccess() {
        mUserInfo.setIs_shop(false);
        mSecurityApplyBusinessChecked.setText(App.INSTANCE.getString(R.string.strSecurityCenterActivity30));
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void updatePath(String path) {
        filePath = path;
    }
}
